<extend name="Public/baseConf"/>
<block name="breadcrumb"> 
	<div class="row wrapper border-bottom white-bg page-heading">
	    <div class="col-lg-6">
	        <h2><present name="data">备份数据库 </h2>
	        <ol class="breadcrumb">
	            <li><a href="{:U('index/index')}">首页</a></li>
	            <li class="active">
	                <strong>备份数据库 </strong>
	            </li>
	        </ol>
	    </div>
	    <div class="col-lg-6">
            <div class="title-action">
				<span class="block pull-right">
					<a  id="export"  class="btn btn-primary" href="javascript:;" autocomplete="off">
						<i class="fa fa-pencil align-top"></i>立即备份
					</a>
					<a id="optimize"  class="btn btn-warning btn-success" href="{:U('optimize')}">
						<i class="fa fa-flag"></i>优化表
					</a>
					<a id="repair" class="btn btn-warning btn-success" href="{:U('repair')}">
						<i class="fa fa-flag"></i>修复表
					</a>
				</span>
            </div>
        </div>
	</div>
</block>
<block name="body">
	<div class="row">
            <div class="col-lg-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>数据列表</h5>
                    <div class="ibox-tools">
                        <a class="collapse-link">
                            <i class="fa fa-chevron-up"></i>
                        </a>
                    </div>
                </div>
                <div class="ibox-content">
                    <div class="table-responsive">
                    <form id="export-form" method="post" action="{:U('export')}">
		                  <table class="table table-striped table-bordered table-hover dataTables-example" >
			                  <thead>
			                  <tr>
		                        <th width="48"><input class="check-all" checked="chedked" type="checkbox" value=""></th>
		                        <th>表名</th>
		                        <th width="120">数据量</th>
		                        <th width="120">数据大小</th>
		                        <th width="160">创建时间</th>
		                        <th width="160">备份状态</th>
		                        <th width="120">操作</th>
			                  </tr>
			                  </thead>
			                  <tbody>
						        <volist name="list" id="table">
			                        <tr>
			                            <td class="num">
			                                <input class="ids" checked="chedked" type="checkbox" name="tables[]" value="{$table.name}">
			                            </td>
			                            <td>{$table.name}</td>
			                            <td>{$table.rows}</td>
			                            <td>{$table.data_length|format_bytes}</td>
			                            <td>{$table.create_time}</td>
			                            <td class="info">未备份</td>
			                            <td class="action">
			                                <a class="ajax-get no-refresh" href="{:U('optimize?tables='.$table['name'])}">优化表</a>&nbsp;
			                                <a class="ajax-get no-refresh" href="{:U('repair?tables='.$table['name'])}">修复表</a>
			                            </td>
			                        </tr>
			                    </volist>
			                  </tbody>
		                  </table>
		            </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <!-- /应用列表 -->
</block>

<block name="script">
    <script type="text/javascript">
    (function($){
        var $form = $("#export-form"), $export = $("#export"), tables
            $optimize = $("#optimize"), $repair = $("#repair");

        $optimize.add($repair).click(function(){
            $.post(this.href, $form.serialize(), function(data){
                if(data.status){
                    updateAlert(data.info,'alert-success');
                } else {
                    updateAlert(data.info,'alert-error');
                }
                setTimeout(function(){
	                $('#top-alert').find('button').click();
	                $(that).removeClass('disabled').prop('disabled',false);
	            },1500);
            }, "json");
            return false;
        });

        $export.click(function(){
            $export.parent().children().addClass("disabled");
            $export.html("正在发送备份请求...");
            $.post(
                $form.attr("action"),
                $form.serialize(),
                function(data){
                    if(data.status){
                        tables = data.tables;
                        $export.html(data.info + "开始备份，请不要关闭本页面！");
                        backup(data.tab);
                        window.onbeforeunload = function(){ return "正在备份数据库，请不要关闭！" }
                    } else {
                        updateAlert(data.info,'alert-error');
                        $export.parent().children().removeClass("disabled");
                        $export.html("立即备份");
                        setTimeout(function(){
        	                $('#top-alert').find('button').click();
        	                $(that).removeClass('disabled').prop('disabled',false);
        	            },1500);
                    }
                },
                "json"
            );
            return false;
        });

        function backup(tab, status){
            status && showmsg(tab.id, "开始备份...(0%)");
            $.get($form.attr("action"), tab, function(data){
                if(data.status){
                    showmsg(tab.id, data.info);

                    if(!$.isPlainObject(data.tab)){
                        $export.parent().children().removeClass("disabled");
                        $export.html("备份完成，点击重新备份");
                        window.onbeforeunload = function(){ return null }
                        return;
                    }
                    backup(data.tab, tab.id != data.tab.id);
                } else {
                    updateAlert(data.info,'alert-error');
                    $export.parent().children().removeClass("disabled");
                    $export.html("立即备份");
                    setTimeout(function(){
    	                $('#top-alert').find('button').click();
    	                $(that).removeClass('disabled').prop('disabled',false);
    	            },1500);
                }
            }, "json");

        }

        function showmsg(id, msg){
            $form.find("input[value=" + tables[id] + "]").closest("tr").find(".info").html(msg);
        }
    })(jQuery);
    </script>
</block>